package com.cyj.service.impl;

import com.cyj.dao.UserDao;
import com.cyj.entity.User;
import com.cyj.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userService")
@Transactional(rollbackFor = {Exception.class})
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public Integer addUser(User user) {
        Map<String,Object> map  = new HashMap<String,Object>();
        map.put("userName",user.getUserName());

        User registerUser = userDao.selectUser(map);

        if(registerUser==null){

            return userDao.addUser(user);
        }else{

            return -1;
        }

    }

    @Override
    public List<User> selectAllUserList(Map map) {
        return userDao.selectUserList(map);
    }

    @Override
    public PageInfo<User> selectUserList(Map map,int pn,int pageSize) {
        PageHelper.startPage(pn,pageSize);

        List<User> list = userDao.selectUserList(map);

        PageInfo<User> pageInfo = new PageInfo<User>(list,5);
        return pageInfo;
    }

    @Override
    public Integer updateUser(User user) {
        return userDao.updateUser(user);
    }

    @Override
    public User selectUser(Map map) {
        return userDao.selectUser(map);
    }
}
